package com.itheima.vms.controller;



import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.vms.domain.PageResult;
import com.itheima.vms.domain.po.Brand;
import com.itheima.vms.response.Result;
import com.itheima.vms.service.IBrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@Tag(name = "品牌管理", description = "品牌管理接口")
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private IBrandService brandService ;

    //查询所有
    @GetMapping
    @Operation(summary = "查询所有品牌")
    public Result<List<Brand>> list() {
        List<Brand> brands = brandService.list();
        return  Result.ok(brands) ;
    }

    //分页查询
    @GetMapping("/page")
    @Operation(summary = "分页查询")
    public Result<PageResult<Brand>> page(Integer page, Integer pageSize){
        // 为page和pageSize设置默认值，防止空指针异常
        int currentPage = (page == null || page <= 0) ? 1 : page;
        int size = (pageSize == null || pageSize <= 0) ? 5 : pageSize;
        Page<Brand> brandPage = brandService.page(new Page<>(currentPage, size), null);
        return Result.ok(new PageResult<>(brandPage.getRecords(),brandPage.getTotal()));
    }

    //新增
    @PostMapping
    @Operation(summary = "新增品牌")
    public Result<Boolean> addBrand(@RequestBody Brand brand){
        return Result.ok(brandService.save(brand));
    }

    //回显查询
    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询商品")
    public Result<Brand> selectById(@PathVariable Integer id){
        return Result.ok(brandService.getById(id));
    }

    //修改
    @PutMapping
    @Operation(summary = "修改品牌")
    public Result<Boolean> updateBrand(@RequestBody Brand brand){
        return Result.ok(brandService.updateById(brand));
    }

    //删除
    @DeleteMapping("/{id}")
    @Operation(summary = "删除品牌")
    private Result<Boolean> deleteBrand(@PathVariable Integer id){
        return Result.ok(brandService.removeById(id));
    }
}